package org.anddev.andengine.extra;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.SimplePreferences;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;

public class Enviroment {
	private static Enviroment mInstance = null;
	
	private Context mContext = null;
	private AudioManager mAudioManager = null;
	
	private int mScreenWidth = 480;
	private int mScreenHeight = 720;
	
	private Enviroment() {
		
	}
	
	public static synchronized Enviroment getInstance() {
		if (mInstance == null)
			mInstance = new Enviroment();
		return mInstance;
	}
	
	public void initContext(Context ctx, int width, int height) {
		this.mContext = ctx;
		
		this.mScreenWidth = width;
		this.mScreenHeight = height;
		
		this.mAudioManager = (AudioManager) this.mContext.getSystemService(Service.AUDIO_SERVICE);
		try {
			getEngine().enableVibrator(this.mContext);
		} catch (Exception e) {
			
		}
	}
	
	public int getScreenWidth() {
		return this.mScreenWidth;
	}
	
	public int getScreenHeight() {
		return this.mScreenHeight;
	}
	
	public void setScreeWidth(int w) {
		this.mScreenWidth = w;
	}
	
	public void setScreeHeight(int h) {
		this.mScreenHeight = h;
	}
	
	public Context getContext() {
        return this.mContext;
	}
	
	public AudioManager getAudioManager() {
        return this.mAudioManager;
	}
	
	public Engine getEngine() {
		return ((BaseGameActivity) getContext()).getEngine();
	}
	
	public Scene getScene() {
		return getEngine().getScene();
	}
	
	public void nextScene() {
		Scene s = getEngine().getScene();
		((FadeLayer) s.getLastChild()).fadeIn();
	}
	
	public void setScene(Scene scene) {
		// need clean object old scene
		//Scene temp = getEngine().getScene();
		getEngine().setScene(scene);
		//temp.detachSelf();
	}
	
	public boolean getAudio() {
		return SimplePreferences.getValue(this.mContext, "audio", true);
	}
	
	public void toggleAudio() {
		boolean value = !(getAudio());
		SimplePreferences.setValue(this.mContext, "audio", value);
	}
	
	public boolean getVibro() {
		return SimplePreferences.getValue(this.mContext, "vibro", true);
	}
	
	public void toggleVibro() {
		boolean value = !(getVibro());
		SimplePreferences.setValue(this.mContext, "vibro", value);
	}
	
}
